package com.roy.DubboRPCDemo;

import java.io.IOException;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
import com.roy.DubboRPCDemo.protocol.MyService;
import com.roy.DubboRPCDemo.protocol.impl.MyServiceImpl;

public class RPCServer {

	private static Logger logger = LoggerFactory.getLogger(RPCServer.class);
	
	public static void main(String[] args) throws IOException {
		ServiceConfig<MyService> serviceConfig = new ServiceConfig<MyService>();

		ApplicationConfig applicationConfig = new ApplicationConfig("MyFirstDubboProvider");
		// qos默认端口22222
		// applicationConfig.setQosPort(20881);
		serviceConfig.setApplication(applicationConfig);

		//zookeeper服务发现
		RegistryConfig registryConfig = new RegistryConfig("zookeeper://localhost:2181");
		//网络发布
//		RegistryConfig registryConfig = new RegistryConfig("multicast://224.5.6.7:1234");
		registryConfig.setTimeout(10);
		/*
		 * dubbo的zookeeper客户端，默认使用的是curator,Netflix开源的一个东东。 maven坐标 
		 * <dependency>
		 * <groupId>com.netflix.curator</groupId>
		 * <artifactId>curator-framework</artifactId> <version>1.1.10</version>
		 * </dependency>
		 * 这里用的还是熟悉的zkclient
		 * 指定客户端可以有两种方式，一是在registryConfig里指定client为zkclient或者curator
		 * 另一种是在协议字符串中指定。 zookeeper://localhost:2181?client=zkclient
		 */
		registryConfig.setClient("zkclient");
		serviceConfig.setRegistry(registryConfig);
		// Protocol
		ProtocolConfig protocol = new ProtocolConfig();
		protocol.setName("dubbo");
		protocol.setPort(12345);
		protocol.setThreads(200);
		serviceConfig.setProtocol(protocol);

		serviceConfig.setInterface(MyService.class);
		serviceConfig.setRef(new MyServiceImpl());
		
		logger.info("=================starting dubbo server===========");
		serviceConfig.export();
		logger.info("=================start dubbo server SUCCESS===========");
		System.in.read();
	}
}
